<template>
  <div>
    <BasicTable @register="registerTable">
      <template #toolbar>
        <ImpExcel @success="excelSuccessFn" dateFormat="YYYY-MM-DD">
          <Button class="m-3" :loading="loading"> 导入读书记录 </Button>
        </ImpExcel>
      </template>
      <template #bodyCell="{ column, record }">
        <template v-if="column.key === 'action'">
          <TableAction
            :actions="[
              {
                icon: 'ant-design:customer-service-outlined',
                onClick: handleView.bind(null, record),
              }
              // {
              //   icon: 'ant-design:delete-outlined',
              //   color: 'error',
              //   popConfirm: {
              //     title: '是否确认删除',
              //     placement: 'left',
              //     confirm: handleDelete.bind(null, record),
              //   },
             // },
            ]"
          />
        </template>
      </template>
    </BasicTable>
    <FormModal @register="register" @success="handleSuccess" />
  </div>
</template>
<script lang="ts" setup>
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { ref,onMounted } from 'vue';
  import { useRoute } from 'vue-router';
  import { userNoteListApi,importUserNote } from '@/api/sys/user';
  const route = useRoute();

  import FormModal from './FormModal.vue';
  import { columns, searchFormSchema } from './role.data';
  import {useMessage} from "@/hooks/web/useMessage";
  import {useModal} from "@/components/Modal";
  import {Button} from "ant-design-vue";
  import {ImpExcel} from "@/components/Excel";


  defineOptions({ name: 'userNoteList' });
  const { createMessage } = useMessage();
  const loading=ref(false);

  const userName = ref(route.params?.userName);

  const [register, { openModal, closeModal }] = useModal();

  const [registerTable, { reload }] = useTable({
    title: '阅读记录列表',
    api: userNoteListApi,
    columns,
    formConfig: {
      labelWidth: 120,
      schemas: searchFormSchema,
      autoSubmitOnEnter: true
    },
    useSearchForm: true,
    showTableSetting: true,
    bordered: true,
    showIndexColumn: false,
    actionColumn: {
      width: 80,
      title: '收听录音',
      dataIndex: 'action',
      // slots: { customRender: 'action' },
      fixed: undefined,
    },
  });

  function handleCreate() {
    openModal(true, {
      isUpdate: false,
    });
  }

  function format(seconds) {//秒转化为分钟格式
    let s=Math.trunc(seconds);
    let minInt=Math.floor(s / 60);
    let min = minInt >= 10 ? minInt : '00' + minInt;
    s -= 60 * min;
    let sec = s >= 10 ? s : '00' + s;
    return  min + ':' + sec;
  }


  function handleEdit(record: Recordable) {
    openModal(true, {
      record,
      isUpdate: true,
    });
  }
  function handleView(record: Recordable) {
   let url=record.mp3Url?record.mp3Url:record.remoteUrl;
    // let url=record.remoteUrl;
    window.open(url)
  }

  function handleDelete(record: Recordable) {
    //console.log(record);

    // deleteApi(record).then(res => {
    //   createMessage.success(res.message);
    //   })
    //   .catch((res) => {
    //     createMessage.error(res.message);
    //   })
    //   .finally(() => {
    //     reload();
    //   });
  }

  function handleSuccess() {
    reload();
  }
  const arrHeader = [
    {
      key: 'log',
      value: '记录',
    },
    {
      key: 'date',
      value: '日期',
    }
  ];


  function getHeadArr(){
    let arr=[];
    arrHeader.forEach(r=>{
      arr.push(r.value)
    })
    return arr;
  }

  function changeSubmitArr(results){
    let data={
      list:[]
    }
    let list=[];
    results.forEach(r=>{
      let json={};
      for(let key in r){
        arrHeader.forEach(e=>{
          if(e.value==key){
            json[e.key]=r[key];
          }
        })
      }
      data.list.push(json)
    });
    return data;
  }




  function excelSuccessFn(list) {
    let results = list[0].results;
    // console.log(results);
    //去除第一行文字提示 shift 删除第一行
    // results.shift();
    let data = changeSubmitArr(results);
    // console.log(data);
    // return;
    loading.value = true;
    importUserNote(data.list)
      .then((res) => {
        loading.value = false;

        if (res.code == 200) {
          if (res.data && res.data.length > 0) {

            //res.data;
          } else {
            createMessage.success('导入成功!');
          //  tableRef.value.commitProxy('reload');
            reload();
          }
        } else {
          loading.value = false;

          createMessage.error(res.message);
        }
      })
      .catch((err) => {
        loading.value = false;
      });
  }

</script>
